package com.qianfeng.zhangwei.sybl.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.qianfeng.zhangwei.sybl.BaseApp;
import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.adapter.GameInfoAdapter;
import com.qianfeng.zhangwei.sybl.bean.Entity;
import com.qianfeng.zhangwei.sybl.bean.Gift;
import com.qianfeng.zhangwei.sybl.config.UrlContants;
import com.qianfeng.zhangwei.sybl.utils.AsyncHttpUtils;

import java.util.ArrayList;

/**
 * 第一步 初始化 ui
 * 第二步 加载数据   --  去网络加载数据   -- 解析
 *
 * 第二步  给ui赋值
 * 1> 自己写 ViewPager  +
 */
public class GameInfoFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {
    private static final String GAME_TYPE = "type";
    private String type;
    private PullToRefreshListView ptrListView;
    private ArrayList<Gift> gifts = new ArrayList<>();
    private GameInfoAdapter adapter;
    private int pageNumber = 1;
    private RequestHandle handle;


    public static GameInfoFragment newInstance(String type) {
        GameInfoFragment fragment = new GameInfoFragment();
        Bundle args = new Bundle();
        args.putString(GAME_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(GAME_TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game_info, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        TypeReference<Entity<Gift>> typeReference = new TypeReference<Entity<Gift>>() {
        };

        handle = AsyncHttpUtils.getInstance().post(UrlContants.URL_GIFT, typeReference, getReqeustParams(), ptrListView, gifts, adapter, pageNumber);
    }

    private RequestParams getReqeustParams() {
        RequestParams params = new RequestParams();
        params.add("uid", "");
        params.add("token", "78fc45f6f%2C7%2C9");
        params.add("platform", "2");
        params.add("gifttype", type);
        params.add("page", String.valueOf(pageNumber));
        return params;
    }

    private void initView(View view) {
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.fragment_gift_ptrlistview);
        adapter = new GameInfoAdapter(gifts, getActivity());
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrListView.setOnItemClickListener(this);
        ptrListView.setAdapter(adapter);
        ptrListView.setOnRefreshListener(this);
        ptrListView.setRefreshing();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNumber = 1;
        if (ptrListView.getMode() != PullToRefreshBase.Mode.BOTH) {
            ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNumber++;
        initData();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BaseApp.getInstance().showToast("----->" + position);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        handle.cancel(true);
    }
}
